package ephrecorder

import (
	"gitee.com/ymofen/gobase"
	"gitee.com/ymofen/golog"
	"github.com/spf13/cobra"
	"time"
)

func init() {
	gobase.ArgFuncRegister("addEphRecorderCmd", gobase.ArgFuncOption{Catalog: "corbaRoot", Func: func(args string, tag interface{}) (val interface{}, err error) {
		var cbrunner CorbaRunner
		var addCorbaTask = &cobra.Command{
			Use: "ephrecorder",
			Run: func(cmd *cobra.Command, args []string) {
				golog.Infof("obsSubConf:%s", cbrunner.ObsSubConf)
				golog.Infof("ephSubConf:%s", cbrunner.EphSubConf)
				golog.Infof("saveFmt:%s", cbrunner.SaveFmt)
				golog.Infof("epochInterval(s):%d", cbrunner.Interval)

				cbrunner.Run()
				gobase.HookExitSignalAndInterval(0, time.Second*10, func() {

				})
			},
		}
		addCorbaTask.Flags().StringVarP(&cbrunner.ObsSubConf, "obsSubConf", "o", "", "")
		addCorbaTask.Flags().StringVarP(&cbrunner.EphSubConf, "ephSubConf", "e", "", "")
		addCorbaTask.Flags().StringVarP(&cbrunner.SaveFmt, "saveFmt", "s", "", "")
		addCorbaTask.Flags().Int64VarP(&cbrunner.Interval, "epochInterval", "i", 120, "")
		return addCorbaTask, nil
	}})

}

type CorbaRunner struct {
	Id         string
	ObsSubConf string
	EphSubConf string
	SaveFmt    string
	Interval   int64
	w          Wrapper
}

func (this *CorbaRunner) Run() error {
	if len(this.Id) == 0 {
		this.Id = "EphRecorder"
	}
	this.w.SaveFmt = this.SaveFmt
	this.w.Interval = this.Interval
	this.w.Init()
	this.w.UpdateConf(this.ObsSubConf, this.EphSubConf)
	return nil
}
